#lang r5rs
(define (last-pair x)
  (if (null? (cdr x))
      (car x)
      (last-pair (cdr x))))
(display (last-pair (list 1 2 3 4)))
(newline)
(define (reverse2 x)
  (if(null? (cdr x))
     (car x)
     (cons (reverse (cdr x)) (car x))))
(define (reverse x)
  (define (re x prev)
    (if(null? (cdr x))
       (cons (car x) prev)
       (re (cdr x) (cons (car x) prev))))
  (re (cdr x) (cons (car x) '()))
  )
(display (reverse (list 1 2 3 4)))
(newline)
(define (same-parity x . z)
  (define (?same-parity n m)
    (= (remainder n 2) (remainder m 2)))
  (define (re-s-parity x y)
    (if(null? y)
       '()
       (if(?same-parity x (car y))
          (cons (car y) (re-s-parity x (cdr y)))
          (re-s-parity x (cdr y)))))
  (cons x (re-s-parity x z)))
(display (same-parity 1 2 3 4 5 6 7))
